package com.rui.cn.consumer.web;

import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.rui.cn.feignservice.SysLogService;
import com.rui.cn.feignservice.SysMenuService;
import com.rui.cn.feignservice.SysRoleService;
import com.rui.cn.entity.system.SysLog;
import com.rui.cn.entity.system.SysMenu;
import com.rui.cn.entity.system.SysRole;
import com.rui.cn.entity.system.SysUser;
import com.rui.cn.util.MenuUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class LoginController {
    //菜单
    @Autowired
    private SysMenuService sysMenuService;
    //日志
    @Autowired
    private SysLogService sysLogService;
    @Autowired
    private SysRoleService sysRoleService;

    /**
     *@ 描述：登录页面
     *@ 方法名：tologin
     *@ 参数：[]
     *@ 返回值类型：ModelAndView
     *@ 创建人：[zhang]
     *@ 创建时间：2018/1/13 0013-下午 23:07
     **/
    @RequestMapping(value = "/")
    public ModelAndView indexpath(){
        ModelAndView modelAndView=new ModelAndView();
        Session session = SecurityUtils.getSubject().getSession();
        if(session.getAttribute("userSession") != null){
            Object object= SecurityUtils.getSubject().getPrincipal();
            SysUser user=new SysUser();
            BeanUtils.copyProperties(object,user);
            List<SysMenu> menuList=sysMenuService.menuList(user.getId());
            List<SysMenu> newList = MenuUtil.sortMenuList(menuList);
            modelAndView.addObject("menu",newList);
            modelAndView.setViewName("system/index");
            return  modelAndView;
        }
        modelAndView.setViewName("login");
        return modelAndView;
    }
    /**
     *@ 描述：登录页面
     *@ 方法名：tologin
     *@ 参数：[]
     *@ 返回值类型：ModelAndView
     *@ 创建人：[zhang]
     *@ 创建时间：2018/1/13 0013-下午 23:07
     **/
    @RequestMapping("/tologin")
    public ModelAndView tologin(){
        return new ModelAndView("login");
    }
    /**
     *@ 描述：退出登录
     *@ 方法名：tologin
     *@ 参数：[]
     *@ 返回值类型：ModelAndView
     *@ 创建人：[zhang]
     *@ 创建时间：2018/1/13 0013-下午 23:07
     **/
    @RequestMapping("/logout")
    public ModelAndView logout(){
        //在这里执行退出系统前需要清空的数据
        Subject subject= SecurityUtils.getSubject();
        try {
            subject.logout();
        }catch (SessionException e){
            e.printStackTrace();
        }
        return new ModelAndView("login");
    }
    /**
     *@ 描述：登录实现
     *@ 方法名：tologin
     *@ 参数：[String username，String password]
     *@ 返回值类型：ModelAndView
     *@ 创建人：[zhang]
     *@ 创建时间：2018/1/13 0013-下午 23:07
     **/
    @RequestMapping(value="/login",method= RequestMethod.POST)
    public ModelAndView login(HttpServletRequest request, String  username, String password){
        ModelAndView modelAndView=new ModelAndView();
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            modelAndView.addObject("message","用户名或密码不能为空！");
            modelAndView.setViewName("login");
            return modelAndView;
        }
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(username,password);
        try {
            subject.login(token);
            //加载菜单
            Object object= SecurityUtils.getSubject().getPrincipal();
            SysUser user=new SysUser();
            BeanUtils.copyProperties(object,user);
            List<SysMenu> menuList=sysMenuService.menuList(user.getId());
            List<SysMenu> newList = MenuUtil.sortMenuList(menuList);
            modelAndView.addObject("menu",newList);
            modelAndView.setViewName("system/index");
            return modelAndView;
        }catch (LockedAccountException lae) {
            token.clear();
            modelAndView.addObject("message","用户已经被锁定不能登录，请与管理员联系！");
            modelAndView.setViewName("login");
        } catch (AuthenticationException e) {
            token.clear();
            modelAndView.addObject("message","用户或密码不正确！");
            modelAndView.setViewName("login");
        }
        return modelAndView;
    }
    /**
     *@ 描述：首页
     *@ 方法名：tologin
     *@ 参数：[]
     *@ 返回值类型：ModelAndView
     *@ 创建人：[zhang]
     *@ 创建时间：2018/1/13 0013-下午 23:07
     **/
    @RequestMapping("/index")
    public ModelAndView index(){
        return new ModelAndView("system/index");
    }
    /**
     *@ 描述：面板页面
     *@ 方法名：main
     *@ 参数：[]
     *@ 返回值类型：org.springframework.web.servlet.ModelAndView
     *@ 创建人：[zhang]
     *@ 创建时间：2018/1/16 0016-下午 21:06
     **/
    @RequestMapping("/main")
    public  ModelAndView main(){
        return new ModelAndView("system/main");
    }
    /**
     *@ 描述：菜单页面
     *@ 方法名：main
     *@ 参数：[]
     *@ 返回值类型：org.springframework.web.servlet.ModelAndView
     *@ 创建人：[zhang]
     *@ 创建时间：2018/1/16 0016-下午 21:06
     **/
    @RequestMapping("/menu")
    public  ModelAndView menu(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("system/menu");
        return modelAndView;
    }
    /**
     *@ 描述：日志页面
     *@ 方法名：log
     *@ 参数：[]
     *@ 返回值类型：org.springframework.web.servlet.ModelAndView
     *@ 创建人：[zhang]
     *@ 创建时间：2018/1/16 0016-下午 21:06
     **/
    @RequestMapping("/log")
    public  ModelAndView log(Pagination pagination, @RequestParam(defaultValue = "30",required = false)Integer size){
        pagination.setSize(size);
        ModelAndView modelAndView=new ModelAndView();
        List<SysLog> logList =sysLogService.logList(pagination);
        modelAndView.addObject("log",logList);
        modelAndView.addObject("page",pagination);
        modelAndView.setViewName("system/log");
        return modelAndView;
    }
    /**
     *@ 描述：用户页面
     *@ 方法名：user
     *@ 参数：[]
     *@ 返回值类型：org.springframework.web.servlet.ModelAndView
     *@ 创建人：[zhang]
     *@ 创建时间：2018/1/16 0016-下午 21:06
     **/
    @RequestMapping("/user")
    public  ModelAndView user(){
        ModelAndView modelAndView=new ModelAndView();
        //加载所有角色信息
        List<SysRole> roleList=sysRoleService.selectList();
        roleList.stream().forEach(System.out::println);
        modelAndView.addObject("role",roleList);
        modelAndView.setViewName("system/user");
        return modelAndView;
    }
}
